<?php
# http://localhost/test/triggers/install-triggers.php
# SELECT CONCAT('DROP TRIGGER ', `TRIGGER_NAME`, ';') FROM `INFORMATION_SCHEMA`.`TRIGGERS` WHERE `TRIGGER_SCHEMA` = 'DATABASE';
$database_username = 'USERNAME';
$database_password = 'PASSWORD';
$database_hostname = 'HOSTNAME';
$database_name = 'DATABASE';
$connection = mysqli_connect($database_hostname, $database_username, $database_password) or die('Connection error');
mysqli_select_db($connection, $database_name) or die('DB Error');

$drop_all_old_triggers_sql="
SELECT
	CONCAT('DROP TRIGGER ', TRIGGERS.TRIGGER_SCHEMA, '.', TRIGGER_NAME, ';') drop_triggers
FROM INFORMATION_SCHEMA.TRIGGERS
WHERE
	TRIGGER_SCHEMA='{$database_name}'
;";
$triggers_rs = mysqli_query($connection, $drop_all_old_triggers_sql);
while($triggers = mysqli_fetch_array($triggers_rs))
{
	mysqli_query($connection, $triggers['drop_triggers']);
}
#die('All triggers cleared!');

$tables = array(
	'test',
);

$skip_tables = array('log_triggers');

$tables=array();
# Skip views
$tables_rs = mysqli_query($connection, "SHOW FULL TABLES WHERE table_type = 'BASE TABLE';");
while($table = mysqli_fetch_array($tables_rs))
{
	$tables[] = $table[0];
}
$tables = array_diff($tables, $skip_tables);
#print_r($tables); die();


$events = array(
	'BEFORE',
	#'AFTER',
);

$actions = array(
	'INSERT',
	'UPDATE',
	'DELETE'
);

foreach($tables as $t => $table)
{
	# Filter
	if(preg_match('/^_/', $table)) continue;
	
	foreach($events as $e => $event)
	{
		foreach($actions as $a => $action)
		{
			$trigger = "t_{$table}_{$event}_{$action}";
			
			echo $clear_sql = "DROP TRIGGER IF EXISTS `{$trigger}`;";
			echo $install_sql = "
CREATE TRIGGER `{$trigger}` {$event} {$action} ON `{$table}`
FOR EACH ROW
BEGIN
	DECLARE _null BOOLEAN;
	DECLARE original_full_query TEXT;
	SET original_full_query = (SELECT info FROM INFORMATION_SCHEMA.PROCESSLIST WHERE id=CONNECTION_ID());
	SELECT f_log('{$table}', '{$event}', '{$action}', original_full_query) INTO _null;
END";
			mysqli_query($connection, $clear_sql) or die('Error');
			mysqli_query($connection, $install_sql) or die('Install error: '.mysqli_error());
			
			flush();
		}
	}
}
?>
Done?